package com.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Test {
    public static void main(String[] args) {
        Student stu =new Student();
       //在不改变student代码的基础上改变eat中的输出语句

        StudentInterface sif=(StudentInterface) Proxy.newProxyInstance(stu.getClass().getClassLoader(), new Class[]{StudentInterface.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getName().equals("study")){
                    System.out.println("同学们爱上了睡觉");
                }else {
                    method.invoke(stu,args);
                }
                return null;
            }
        });
        sif.eat("mifan");
        sif.study();
    }
}
